﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;
namespace BusinessLogicLayer.Manager
{
    public class BookmarkManager : ManagerBase<BookmarkDataProvider>
    {
        #region [Constructor]
         /// <summary>
        /// Initializes a new instance of the <see cref="BookmarkManager" /> class.
        /// </summary>
        public BookmarkManager() { }
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new BookmarkDataProvider();
        }

        #endregion
        #region [Public Methods]
        /// <summary>
        /// Update or insert a bookmark.
        /// </summary>
        /// <param name="bookmark">bookmark object</param>
        /// <returns>true or false</returns>
        public bool UpdateBookmark(Bookmark bookmark)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (bookmark.UserID > 0 && bookmark.ArticleID > 0)
                {
                    //Update the bookmark
                    success = base.DataProvider.UpdateBookmark(bookmark.UserID, bookmark.ArticleID, bookmark.IsActive);
                }
                else
                {
                    //Insert an bookmark
                    success = base.DataProvider.InsertBookmark(bookmark.UserID, bookmark.ArticleID, bookmark.IsActive);

                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }
        
        #endregion

        #region [Private Methods]
        /// <summary>
        /// Fill the bookmark object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>the bookmark list</returns>
        private List<Bookmark> FillBookmarkObjectsByEntities(AutoFixDataSet.BookmarkArticleDataTable dataTable)
        {
            //The bookmark list that contains the objects
            //Loop through the data table and create new BookmarkObject 
            //finally select the BookmarkObject and convert to list
            List<Bookmark> bookmarkDetails = (from bo in
                                                (from b in dataTable.AsEnumerable<AutoFixDataSet.BookmarkArticleRow>()
                                                 select new
                                                 {
                                                     BookmarkObject = new Bookmark()
                                                     {
                                                         UserID = b.UserID,
                                                         ArticleID = b.ArticleID,                                                         
                                                         IsActive = b.IsActive
                                                     }
                                                 })
                                            select bo.BookmarkObject).ToList();
            //Return the filled user details list
            return bookmarkDetails;
        }
        #endregion
    }
}
